import axios from "axios";

const checkStatus = (response) => {
    const status = response.status || -1000; // -1000 自己定义，连接错误的status
    if ((status >= 200 && status < 300) || status === 304) {
      // 如果http状态码正常，则直接返回数据
      return response.data;
    }
    return {
      status,
    };
  },
  // 创建axios实例
  instance = axios.create();

instance.defaults.baseURL = "";

instance.defaults.headers.post["Content-Type"] = "application/json";

instance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    Promise.error(error);
  }
);

instance.interceptors.response.use(
  (response) => {
    // response.status === 200
    //   ? Promise.resolve(response.data)
    //   : Promise.reject(response);
    // if (response.status === 200) {
    //   Promise.resolve(response.data)
    // } else {
    //   Promise.reject(response);
    // }
    return Promise.resolve(checkStatus(response));
  },
  (error) => {
    const { response } = error;
    if (response) {
      // 可统一拦截并处理错误
    } else {
      // throw error;
    }
  }
);

export default instance;
